﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using INFDTM01.Services;
using INFDTM01.Models;
using INFDTM01.ViewModels;
using System.Diagnostics;

namespace INFDTM01.Controllers
{
    public class UserController : Controller
    {

        public ActionResult CreateStandardUsers()
        {
            UserRecommendationService userRecommendationService = new UserRecommendationService();
            UserPredictionService userPredictionService = new UserPredictionService();

            Dictionary<int, UserPreference> userPreferences = userRecommendationService.CreateStandardUsers();
            Dictionary<int, double> userSimilarities = userRecommendationService.calculateSimilarities(7, userPreferences);
            Dictionary<int, double> rankings = userRecommendationService.calculateRanking(userSimilarities, userPreferences, 7);
            
            Dictionary<int, double> sortedRankings = (from entry in rankings orderby entry.Value descending select entry)
                .ToDictionary(pair => pair.Key, pair => pair.Value);

            Dictionary<int, float> predictions = userPredictionService.Predict(userPreferences, 7);

            CreateStandardUsers viewModel = new CreateStandardUsers();
            viewModel.UserPreferences = userPreferences;
            viewModel.UserSimilarities = userSimilarities;
            viewModel.Rankings = sortedRankings;
            viewModel.Predictions = predictions;

            return View(viewModel);
        }

        public ActionResult CreateUsersFromFile()
        {
            Stopwatch stopwatch1 = new Stopwatch();
            stopwatch1.Start();

            UserRecommendationService userService = new UserRecommendationService();
            UserPredictionService userPredictionService = new UserPredictionService();

            Dictionary<int, UserPreference> userPreferences = userService.CreateUsersFromFile();

            stopwatch1.Stop();

            Stopwatch stopwatch2 = new Stopwatch();
            stopwatch2.Start();

            Dictionary<int, double> userSimilarities = userService.calculateSimilarities(22, userPreferences);

            Dictionary<int, double> topSimilarities = (from pair in userSimilarities orderby pair.Value descending select pair).Take(10).ToDictionary(x => x.Key, x => x.Value);

            Dictionary<int, double> rankings = userService.calculateRanking(topSimilarities, userPreferences, 22);

            Dictionary<int, double> sortedRankings = (from entry in rankings orderby entry.Value descending select entry)
                .ToDictionary(pair => pair.Key, pair => pair.Value);

            stopwatch2.Stop();

            CreateUsersFromFile viewModel = new CreateUsersFromFile();
            viewModel.UserPreferences = userPreferences;
            viewModel.UserSimilarities = topSimilarities;
            viewModel.Rankings = sortedRankings;

            viewModel.ReadingTimeInSeconds = stopwatch1.Elapsed.Seconds;
            viewModel.ReadingTimeInMilliSeconds = stopwatch1.ElapsedMilliseconds;

            viewModel.CalculationInSeconds = stopwatch2.Elapsed.Seconds;
            viewModel.CalculationInMilliSeconds = stopwatch2.ElapsedMilliseconds;

            return View(viewModel);
        }

        public ActionResult SearchMovie(int id)
        {
            UserRecommendationService userService = new UserRecommendationService();

            Movie movie = userService.SearchMovie(id);

            CreateMoviesFromFile viewModel = new CreateMoviesFromFile();
            viewModel.Movie = movie;

            return View(viewModel);
        }

        public ActionResult CreateUsersAndPredict()
        {
            UserRecommendationService userService = new UserRecommendationService();
            UserPredictionService userPredictionService = new UserPredictionService();
            int userId = 22;

            Dictionary<int, UserPreference> userPreferences = userService.CreateUsersFromFile();
            Dictionary<int, float> predictions = userPredictionService.Predict(userPreferences, userId);

            CreateUsersFromFile viewModel = new CreateUsersFromFile();
            viewModel.Predictions = predictions;
            viewModel.UserId = userId;

            return View(viewModel);
        }
    }
}
